<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Common_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function get_username_id($id)
	{
		//$this->db->select('username');
		$this->db->from('users');
		$this->db->where('id ='.$id);
		return $this->db->get()->row()->username;
		//$this->db->get_where('users',array('id'=>$id));
		
		//return $userid = $id;
	}
	
	public function get_taskgroupname_by_id($id)
	{
		$this->db->select('name');
		$this->db->from('task_groups');
		$this->db->where('id ='.$id);
		return $this->db->get()->row_array();
	}
	
	public function get_menu_by_group_id($id,$hid){
		$this->db->select('menu_access.*,menu.*');
		//$this->db->distinct('menu.*');
		$this->db->from('menu_access');
		$this->db->where('group_id ='.$id);
		$this->db->where('parent = 0');
		$this->db->where('heading_id ='.$hid);
		//$this->db->where('heading = 1');
		$this->db->join('menu', 'menu_access.menu_id = menu.id');
		$this->db->order_by('ordering');	
		return $this->db->get()->result_array();
		//return $this->db->get_where('menu_access',array())->result();
	}
	
	public function get_menu_heading($id){
		$this->db->select('menu_heading_access.*,menu_heading.*');
		//$this->db->distinct('menu.*');
		$this->db->from('menu_heading_access');
		$this->db->where('group_id ='.$id);	
		//$this->db->where('heading = 1');
		$this->db->join('menu_heading', 'menu_heading_access.heading_id = menu_heading.id');
		$this->db->order_by('ordering');	
		return $this->db->get()->result();
		//return $this->db->get_where('menu_access',array())->result();
	}
	
	/**
	* child menu
	* @param undefined $user_id
	* @param undefined $menu_id
	* 
*/

public function get_child_menu_by_group_id($id,$mid){
		$this->db->select('menu_access.*,menu.*');
		//$this->db->distinct('menu.*');
		$this->db->from('menu_access');
		$this->db->where('group_id ='.$id);
		$this->db->where('parent ='.$mid);
		//$this->db->where('heading_id ='.$hid);
		//$this->db->where('heading = 1');
		$this->db->join('menu', 'menu_access.menu_id = menu.id');
		$this->db->order_by('ordering');	
		return $this->db->get()->result();
		//return $this->db->get_where('menu_access',array())->result();
	}
	
	public function get_child_menu_by_id($user_id,$menu_id){
		$this->db->select('menu_access.*,menu.*');
		$this->db->from('menu_access');
		$this->db->where('group_id ='.$user_id);
		$this->db->where('parent ='.$menu_id);
		$this->db->join('menu', 'menu_access.menu_id = menu.id');	
		return $this->db->get()->result();
	}
	
	public function get_user_group($id=null){
		if(isset($id))
		{
			$user_id = $id;
		}
		else{
			$user_id = $this->session->userdata('user_id');
		}
		return $this->db->get_where('users_groups',array('user_id'=>$user_id))->row()->group_id;
	}
	
	public function get_client_name_by_id($client_id){
		$this->db->select('users.*');
		$this->db->from('users');
		$this->db->where('users.id ='. $client_id);
		return $this->db->row()->username;
	}
	
	public function get_menu_heading_ID($url){
		$this->db->select('heading_id');
		$this->db->from('menu');
		$this->db->where("url LIKE '%{$url}%'");
		$query = $this->db->get()->row();
		if($query){		
		//return $this->db->affected_rows();
		if ($this->db->affected_rows() > 0)
			{
			  return $query->heading_id;
			}
			else
			{
			  return FALSE;
			}
		}
		/*if($query){
			return $query->row()->heading_id;
		}*/
		//$this->db->get();
		//return $this->db->last_query();
	}
	
	public function is_active($id){
		return $this->db->get_where('users',array('id'=>$id))->row()->active;
	}
	function get_server_time()
	{
		$query = $this->db->query("select CURTIME()")->row_array();
		return $query['CURTIME()'];	
	}
	
	public function get_drinks(){
		$check['month_id'] = date('m');
		$check['date_id'] = date('d');
		$check['user_id'] = $this->session->userdata('user_id');		
		$id =  $this->db->get_where('attendance_timings',$check)->row_array();		
		if(!empty($id)){		
		if($drinks = $this->db->get_where('drinks',array('attendance_timings_id'=>$id['id']))->row_array()){
			return $drinks['drink'];
		}else{
			return FALSE;
		}}	
	}
	
	public function get_notification($limit=NULL){		
		$this->db->select('*');
		$this->db->from('notification');
		$this->db->where('user_id',$this->session->userdata('user_id'));		
		$this->db->order_by('on','DESC');
		$this->db->limit(10);
		return $this->db->get()->result();
	}
	
	public function get_unread_notification($limit=NULL){		
		$this->db->select('*');
		$this->db->from('notification');
		$this->db->where('notify_to',$this->session->userdata('user_id'));		
		$this->db->order_by('on','DESC');
		$this->db->limit(10);
		return($this->db->get()->result());
		//return $this->db->order_by( 'id','DESC')->get_where('notification',array('notify_to'=>$this->session->userdata('user_id')))->result();
	}
	
}